package com.workflow.generator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.DirectedWeightedMultigraph;

public class IOMethods {
	private InputStreamReader isr = new InputStreamReader(System.in);
	private BufferedReader br = new BufferedReader(isr);

	// Method to create and initialise a graph
	public DirectedWeightedMultigraph<String, DefaultWeightedEdge> initializeAGraph(
			DirectedWeightedMultigraph<String, DefaultWeightedEdge> graph)
			throws NumberFormatException, IOException {
		int nVertices, nEdges;
		System.out
				.println("**Creating a Directed Weighted Graph to simulate a networked environment**");
		System.out.println("Number of nodes: ");
		nVertices = Integer.parseInt(br.readLine());
		System.out.println("Number of edges:");
		nEdges = Integer.parseInt(br.readLine());

		// Add vertex to graph
		for (int i = 1; i <= nVertices; i++) {
			graph.addVertex(Integer.toString(i));
		}

		// Add edges
		for (int i = 0; i < nEdges; i++) {
			int source, destination;
			double distance;
			// DefaultWeightedEdge edge;
			System.out.println("Enter the details for edge " + (i + 1) + " :");
			System.out.print("Source Node : ");
			source = Integer.parseInt(br.readLine());
			System.out.print("Destination Node : ");
			destination = Integer.parseInt(br.readLine());
			System.out.print("Distance : ");
			distance = Double.parseDouble(br.readLine());

			// Add the edge to the graph with the specified distance

			addEdges(graph, Integer.toString(source),
					Integer.toString(destination), distance);
			addEdges(graph, Integer.toString(destination),
					Integer.toString(source), distance);
		}

		return graph;
	}

	private DirectedWeightedMultigraph<String, DefaultWeightedEdge> addEdges(
			DirectedWeightedMultigraph<String, DefaultWeightedEdge> graph,
			String source, String destination, Double weight) {
		DefaultWeightedEdge edge;
		graph.addEdge(source, destination);
		edge = graph.getEdge(source, destination);
		graph.setEdgeWeight(edge, weight);

		return graph;
	}

	// Method to print distance matrix
	public void getDistanceMatrix(
			DirectedWeightedMultigraph<String, DefaultWeightedEdge> graph) {
		PathSearchingEngines engine = new PathSearchingEngines();
		int size = graph.vertexSet().size();

		for (int i = 1; i <= size; i++) {
			for (int j = 1; j <= size; j++) {
				System.out.print(engine.getDistance(graph, Integer.toString(i),
						Integer.toString(j)) + "  ");
			}
			System.out.println();
		}
	}

	// Method to print neighbour matrix
	public void getNeighbourMatrix(
			DirectedWeightedMultigraph<String, DefaultWeightedEdge> graph) {
		PathSearchingEngines engine = new PathSearchingEngines();
		int size = graph.vertexSet().size();
		for (int i = 1; i <= size; i++) {
			for (int j = 1; j <= size; j++) {
				System.out.print(engine.isNeighbourOf(graph,
						Integer.toString(i), Integer.toString(j))
						+ "  ");
			}
			System.out.println();
		}
	}
}